/**
 * 
 */
package TestCases;

import java.awt.Point;
import java.io.IOException;
import java.util.Date;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import Tracer.RTBitmap;
import Tracer.RTCanvas;
import Tracer.RTCoreTracing;
import Tracer.RTVector;
import Tracer.ThreadPool;
import XMLScene.RTCamera;
import XMLScene.RTImportXML;
import junit.framework.TestCase;

/**
 * @author Eagle
 * 
 */
public class RTRayTest extends TestCase {

	/**
	 * @param name
	 */
	public RTRayTest(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Test method for objectListHitDetect Test it works well after performing
	 * objectListHitDetect
	 * 
	 * @throws SAXException
	 * @throws IOException
	 */
	public void testObjectListHitDetect() throws SAXException, IOException {
		String outputFileName = null;
		RTCanvas canvas = new RTCanvas();
		int threadNum = 1;

		XMLReader parser = XMLReaderFactory.createXMLReader();
		RTImportXML importXML = new RTImportXML();

		parser.setFeature("http://xml.org/sax/features/validation", true);
		parser.setContentHandler(importXML);
		parser.parse("test1.xml");
		RTCamera camera = importXML.getCamera();

		RTVector Ce = camera.getLocationVector();
		RTVector c = RTVector.normaliseVectors(RTVector.sumVectors(camera
				.getLookatVector(), RTVector.reverseVector(Ce)));
		RTVector Cr = RTVector.normaliseVectors(RTVector.crossProductVectors(
				camera.getSkyVector(), c));
		RTVector Cd = RTVector.normaliseVectors(RTVector.crossProductVectors(
				Cr, c));
		RTVector relativeWH = RTVector.sumVectors(RTVector.multipleVectors(Cr,
				(canvas.getWidth() / 2)), RTVector.multipleVectors(Cd, (canvas
				.getHeight() / 2)));
		RTVector Cc = RTVector.sumVectors(RTVector.sumVectors(Ce, RTVector
				.multipleVectors(c, canvas.getZoom())), RTVector
				.reverseVector(relativeWH));

		canvas.setCornerUL(Cc);
		canvas.setRightD(Cr);
		canvas.setDownD(Cd);

		RTBitmap resultBitmap = new RTBitmap((int) canvas.getXpix(),
				(int) canvas.getYpix());
		int xUnits = canvas.getXpix();
		int yUnits = canvas.getYpix();
		// Add the threads to parallelize
		Point[][] splitedRayList = new Point[yUnits][xUnits];
		for (int i = 0; i < yUnits; i++) {
			for (int j = 0; j < xUnits; j++) {
				splitedRayList[i][j] = new Point();
				splitedRayList[i][j].setLocation(j, i);
			}
		}
		Date startTime = new Date();
		long l1 = startTime.getTime();
		ThreadPool threadPool = new ThreadPool(threadNum);
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < yUnits; i++) { // create yUnits tasks
			threadPool.execute(new RTCoreTracing(splitedRayList[i], importXML,
					resultBitmap,canvas));
		}
		threadPool.waitFinish(); // wait until all tasks finish
		threadPool.closePool(); // close thread pool

		Date completeTime = new Date();
		long l2 = completeTime.getTime();
		long difference = l2 - l1;
		if (canvas.getIsCalTraceTime()) {
			System.out.println("Tracetime: " + difference + " ms");
		}

		resultBitmap.writeBitmapToBinaryPPM(outputFileName);
		System.out.println("done!");
	}
}
